package net.maku.generator.utils;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

/**
 * ID生成器 工具类
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
public class IDUtils {

    private static final long machineId = 1;
    private static final AtomicLong sequence = new AtomicLong(0);

    private static final long maxSequence = 4095;

    public static String generateID(){
        long timestamp = System.currentTimeMillis();
        long seq = sequence.getAndIncrement() & maxSequence;

        if (seq == 0) {
            while (System.currentTimeMillis() <= timestamp){}
        }
        return Long.toHexString(timestamp) + Long.toHexString(machineId) + Long.toHexString(seq);
    }

    public static String generateUUID(){
        return UUID.randomUUID().toString();
    }

    public static String generateSimpleUUID(){
        return UUID.randomUUID().toString().replaceAll("_","");
    }
}
